package com.ymjspace.util;

import org.springframework.stereotype.Component;

import javax.sound.midi.Soundbank;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Description
 * @Author YMJ
 * @DateTime 2020-07-26 10:51
 * @Version V1.0.0
 */
@Component
public class MySqlConectionFactory {
    private MySqlConectionFactory(){}

    enum MySqlConectionInstance{
        INSTANCE;
        private Connection connection;
        private MySqlConectionInstance(){
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            try {
                connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/pfo?characterEncoding=utf-8&serverTimezone=Hongkong","root","123456");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        public Connection getConectionInstance(){
            return connection;
        }
    };

    public ResultSet getResult(String sql){
        ResultSet resultSet = null;
        try {
            resultSet = MySqlConectionInstance.INSTANCE.getConectionInstance().prepareStatement(sql).getResultSet();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultSet;

    }

    public static void main(String[] args) {
        System.out.printf(MySqlConectionInstance.INSTANCE.getConectionInstance().toString());
    }
}
